package com.pioneer.controller.purchase;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.purchase.domain.PurOrder;
import com.pioneer.web.purchase.service.IPurOrderService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 采购订单
 *
 * @author hlm
 */
@RestController
@RequestMapping("/purchase/order")
public class PurOrderController extends BaseController {

    @Resource
    private IPurOrderService orderService;

    /**
     * 获取采购订单列表
     *
     * @param order 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:order:list')")
    @GetMapping("/list")
    public AjaxResult list(PurOrder order) {
        boolean isPage = startPage();
        List<PurOrder> list = orderService.list(Wrappers.query(order));
        return success(isPage, list);
    }

    /**
     * 根据采购订单id获取详细信息
     *
     * @param orderId 采购订单id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable Long orderId) {
        return AjaxResult.success(orderService.selectOrderById(orderId));
    }

    /**
     * 修改采购订单
     *
     * @param order 采购订单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:order:edit')")
    @Log(title = "采购订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PurOrder order) {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 提交采购订单
     * @param order 采购订单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:order:commit')")
    @Log(title = "采购订单", businessType = BusinessType.OTHER)
    @PostMapping("/commit")
    public AjaxResult commit(@RequestBody PurOrder order) {
        return toAjax(orderService.commitOrder(order));
    }

    /**
     * 审批采购订单
     * @param order 采购订单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:order:approve')")
    @Log(title = "采购订单", businessType = BusinessType.OTHER)
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody PurOrder order) {
        return toAjax(orderService.approveOrder(order));
    }
}
